package searchtab.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

import searchtab.SearchTabActivator;

public class SearchTabView extends ViewPart {

    /**
     * The ID of the view as specified by the extension.
     */
    public static final String ID = "searchtab.views.SearchTabView";

    private SearchTab searchTab;

    /**
     * The constructor.
     */
    public SearchTabView() {
        SearchTabActivator.getDefault().getSearcherEngine().setView(this);
    }

    /**
     * This is a callback that will allow us to create the viewer and initialize it.
     */
    @Override
    public void createPartControl(Composite parent) {

        Composite composite = new Composite(parent, SWT.NONE);

        GridLayout layout = new GridLayout();
        layout.horizontalSpacing = 0;
        layout.verticalSpacing = 0;
        layout.marginWidth = 0;
        layout.marginHeight = 0;
        composite.setLayout(layout);

        createViewer(composite);
        getSite().getPage().addPartListener(new SearchTabViewListener());
    }

    private void createViewer(Composite parent) {

        searchTab = new SearchTab(parent, this);

        searchTab.getFilterControl().addModifyListener(SearchTabActivator.getDefault().getSearcherEngine());

    }

    /**
     * Passing the focus request to the viewer's control.
     */
    @Override
    public void setFocus() {
        searchTab.getFilterControl().setFocus();
        searchTab.getFilterControl().selectAll();
    }

    public String getFilterText() {
        return searchTab.getFilterControl().getText();
    }

    public Text getFilterControl() {
        return searchTab.getFilterControl();
    }

}